package lib.persistence.commands;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import models.*;

public class MakeCSV {
		public String checkForFile(String filename) throws IOException
		{
			File f = new File(filename.concat(".csv"));
			if (f.createNewFile()) 
			{
				return filename.concat(".csv");
			}
			else 
			{
				File dir = new File("/");
				String[] files = dir.list();
				int numFiles = files.length;
				for(int i = 0; i < numFiles; i++)
				{
					String newname = filename.concat(Integer.toString(i)).concat(".csv");
					File newfile = new File(newname);
					if(newfile.createNewFile())
					{
						return newname;
					}
				}
			}
			return filename;
		}
	
		public void makeProjectReport(List<Project> p) throws IOException
		{
			try{
				float avg_comp = 0;		//used to compute average completion rate
				String filename = checkForFile("project_report");
				FileWriter writer = new FileWriter(filename, false);
				//append in all field names we want in the report
				writer.append("Name");
				writer.append(",");
				writer.append("Number of Milestones");
				writer.append(",");
				writer.append("Number of Milestones Completed");
				writer.append(",");
				writer.append("Number of Deliverables");
				writer.append(",");
				writer.append("Number of Deliverables Completed");
				writer.append(",");
				writer.append("Total Completion Rate");
				writer.append(",");
				writer.append("Expected Completion Date");
				writer.append(",");
				writer.append("Final Completion Date");
				writer.append(",");
				writer.append("Status");
				writer.append("\n");
				//now we append in the data from the list of projects
				for(int i = 0; i < p.size(); i++)
				{
					writer.append(p.get(i).getName());
					writer.append(",");
					writer.append(Integer.toString(p.get(i).getNumber_Milestones()));
					writer.append(",");
					writer.append(Integer.toString(p.get(i).getCurrentMilestonesCompleted()));
					writer.append(",");
					writer.append(Integer.toString(p.get(i).getNumber_deliverables()));
					writer.append(",");
					writer.append(Integer.toString(p.get(i).getCurrentDeliverablesCompleted()));
					writer.append(",");
					writer.append(Double.toString(p.get(i).getTotalCompletionRate()));
					avg_comp = (float) (avg_comp + p.get(i).getTotalCompletionRate());
					writer.append(",");
					writer.append(p.get(i).getExpectedCompletionDate().toString());
					writer.append(",");
					writer.append(p.get(i).getFinalCompletionDate().toString());
					writer.append(",");
					writer.append(p.get(i).getStatus().toString());
					writer.append("\n");
				}
				//now we append the average of the completion rates
				avg_comp = avg_comp / p.size();
				writer.append("\n");
				writer.append("Average Completion Rate");
				writer.append(",");
				writer.append(Float.toString(avg_comp));
				writer.flush();
				writer.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
		public void makePersonReport(ArrayList<Person> p){
			try{
				String filename = checkForFile("person_report");
				FileWriter writer = new FileWriter(filename, false);
				//append in all field names we want in the report
				writer.append("Employee ID");
				writer.append(",");
				writer.append("Last Name");
				writer.append(",");
				writer.append("First Name");
				writer.append(",");
				writer.append("Gender");
				writer.append(",");
				writer.append("Title");
				writer.append(",");
				writer.append("Date Hired");
				writer.append(",");
				writer.append("Phone Number");
				writer.append(",");
				writer.append("Description");
				writer.append("\n");
				//now we append in the data from the list of people passed in
				for(int i = 0; i < p.size(); i++)
				{
					writer.append(p.get(i).getEmployeeNumber());
					writer.append(",");
					writer.append(p.get(i).getLastName());
					writer.append(",");
					writer.append(p.get(i).getFirstName());
					writer.append(",");
					writer.append(p.get(i).getSex().toString());
					writer.append(",");
					writer.append(p.get(i).getTitle());
					writer.append(",");
					writer.append(p.get(i).getCreatedAt().toString());
					writer.append(",");
					writer.append(p.get(i).getPhone());
					writer.append(",");
					writer.append(p.get(i).getDescription());
					writer.append("\n");
				}
				writer.flush();
				writer.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
		public void makePublicationReport(ArrayList<Publication> p){
			try{
				String filename = checkForFile("publication_report");
				FileWriter writer = new FileWriter(filename, false);
				//append in all field names we want in the report
				writer.append("Title");
				writer.append(",");
				writer.append("Authors");
				writer.append(",");
				writer.append("Publication Date");
				writer.append(",");
				writer.append("Location");
				writer.append(",");
				writer.append("Description");
				writer.append("\n");
				//now we append in the data from the list of people passed in
				for(int i = 0; i < p.size(); i++)
				{
					writer.append(p.get(i).getTitle());
					writer.append(",");
					String authors = new String();
					String fname = new String();
					String lname = new String();
					for(int j = 0; j < p.get(i).getAuthors().size(); j++)
					{
						fname = p.get(i).getAuthors().get(j).getFirstName();
						lname = p.get(i).getAuthors().get(j).getLastName();
						fname = fname.concat(" ".concat(lname));
						authors = authors.concat(fname.concat(" / "));
					}
					writer.append(authors);
					writer.append(",");
					writer.append(p.get(i).getPublicationDate().toString());
					writer.append(",");
					writer.append(p.get(i).getLocation());
					writer.append(",");
					writer.append(p.get(i).getDescription());
					writer.append("\n");
				}
				writer.flush();
				writer.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
		public void makeMilestoneReport(ArrayList<Milestone> m){
			try{
				String filename = checkForFile("milestone_report");
				FileWriter writer = new FileWriter(filename, false);
				int totalcompleted = 0;
				float avgcompleted = 0;
				//append in all field names we want in the report
				writer.append("Title");
				writer.append(",");
				writer.append("Milestone Number");
				writer.append(",");
				writer.append("Number Of Deliverables");
				writer.append(",");
				writer.append("Creation Date");
				writer.append(",");
				writer.append("Completion Date");
				writer.append(",");
				writer.append("Projected Completion Date");
				writer.append(",");
				writer.append("Created By");
				writer.append(",");
				writer.append("Status");
				writer.append("\n");
				//now we append in the data from the list of people passed in
				for(int i = 0; i < m.size(); i++)
				{
					writer.append(m.get(i).getTitle());
					writer.append(",");
					writer.append(Integer.toString(m.get(i).getMilestoneNumber()));
					writer.append(",");
					writer.append(Integer.toString(m.get(i).getNumberDeliverables()));
					writer.append(",");
					writer.append(m.get(i).getCreationDate().toString());
					writer.append(",");
					writer.append(m.get(i).getCompletionDate().toString());
					writer.append(",");
					writer.append(m.get(i).getProjectedCompletion().toString());					
					writer.append(",");
					writer.append(m.get(i).getCreator().getFirstName().concat(" ".concat(m.get(i).getCreator().getLastName())));
					writer.append(",");
					writer.append(m.get(i).getStatus().toString());
					if(m.get(i).getStatus().toString().compareToIgnoreCase("Complete") == 0)
							{
								totalcompleted++;
							}
					writer.append("\n");
				}
				avgcompleted = totalcompleted / m.size();
				writer.append("\n");
				writer.append("Total Completed Milestones");
				writer.append(",");
				writer.append(Integer.toString(totalcompleted));
				writer.append("\n");
				writer.append("Average Completion");
				writer.append(",");
				writer.append(Float.toString(avgcompleted * 100));
				writer.flush();
				writer.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
		public void makeDeliverableReport(List<Deliverable> d){
			try{
				String filename = checkForFile("deliverable_report");
				FileWriter writer = new FileWriter(filename, false);
				double totalcompleted = 0;
				double avgcompleted = 0;
				//append in all field names we want in the report
				writer.append("Title");
				writer.append(",");
				writer.append("Deliverable Number");
				writer.append(",");
				writer.append("Related Project");
				writer.append(",");
				writer.append("Creation Date");
				writer.append(",");
				writer.append("Last Updated");
				writer.append(",");
				writer.append("Status");
				writer.append(",");
				writer.append("Additional Notes");
				writer.append("\n");
				//now we append in the data from the list of people passed in
				for(int i = 0; i < d.size(); i++)
				{
					writer.append(d.get(i).getTitle());
					writer.append(",");
					writer.append(Integer.toString(d.get(i).getDeliverableNumber()));
					writer.append(",");
					writer.append(d.get(i).getProject().getName());
					writer.append(",");
					writer.append(d.get(i).getCreationDate().toString());
					writer.append(",");
					writer.append(d.get(i).getUpdatedAt().toString());
					writer.append(",");
					writer.append(d.get(i).getStatus().toString());
					if(d.get(i).getStatus().toString().equalsIgnoreCase("Complete"))
								totalcompleted++;
					writer.append(",");
					writer.append(d.get(i).getAdditionalNotes());
					writer.append("\n");
				}
				avgcompleted = (totalcompleted / d.size()) * 100;
				writer.append("\n");
				writer.append("Total Completed Deliverables");
				writer.append(",");
				writer.append(Double.toString(totalcompleted));
				writer.append("\n");
				writer.append("Average Completion");
				writer.append(",");
				writer.append(Double.toString(avgcompleted));
				writer.flush();
				writer.close();

			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
		public void makeEventReport(ArrayList<Event> e){
			try{
				String filename = checkForFile("event_report");
				FileWriter writer = new FileWriter(filename, false);
				int totalcompleted = 0;
				float avgcompleted = 0;
				//append in all field names we want in the report
				writer.append("Event Name");
				writer.append(",");
				writer.append("Date");
				writer.append(",");
				writer.append("Location");
				writer.append(",");
				writer.append("Related Project");
				writer.append(",");
				writer.append("Status");
				writer.append(",");
				writer.append("Additional Notes");
				writer.append("\n");
				//now we append in the data from the list of people passed in
				for(int i = 0; i < e.size(); i++)
				{
					writer.append(e.get(i).getEventName());
					writer.append(",");
					writer.append(e.get(i).getEventDate().toString());
					writer.append(",");
					writer.append(e.get(i).getLocation());
					writer.append(",");
					writer.append(e.get(i).getProject().getName());
					writer.append(",");
					writer.append(e.get(i).getStatus().toString());
					if(e.get(i).getStatus().toString().compareToIgnoreCase("Complete") == 0)
					{
						totalcompleted++;
					}
					writer.append(",");
					writer.append(e.get(i).getAdditionalNotes());
					writer.append("\n");
				}
				avgcompleted = (totalcompleted / e.size()) * 100;
				writer.append("\n");
				writer.append("Total Completed Events");
				writer.append(",");
				writer.append(Integer.toString(totalcompleted));
				writer.append("\n");
				writer.append("Average Completion");
				writer.append(",");
				writer.append(Float.toString(avgcompleted));
				writer.flush();
				writer.close();
			}
			catch(IOException ex)
			{
				ex.printStackTrace();
			}
		}
}
